package com.relax.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.relax.AdminLoginResultDTO;
import com.relax.service.KafkaProducerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.UriUtils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
public class DipuController {

    public static Map<String,Integer> map = new HashMap<>();
    public static Map<String,Integer> statusMap = new HashMap<>();


    /**
     * 文件下载
     * @param body
     * @return
     */
    @GetMapping("/ipc/restful/downloadReport")
    public void down(@RequestBody String body, HttpServletResponse response) {// @RequestBody String body
        String initialFilePath ="/opt/microservices" + File.separator + "aa.docx";
        String reportName = UUID.randomUUID().toString()+".docx";
        response.setContentType("application/aa.docx");
        try {
            // 设置响应头
//            response.setContentType("application/zip");
            String name = UriUtils.encode(reportName, StandardCharsets.UTF_8.name());
            response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + name);
            InputStream input = Files.newInputStream(Paths.get(initialFilePath));
            ServletOutputStream os = response.getOutputStream();
            StreamUtils.copy(input, os);
            os.flush();
        } catch (Exception e) {

        }
    }


    @PutMapping("/ipc/restful/mission")
    public Object start(@RequestBody String body) {
        System.out.println("start======="+body);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",true);
        jsonObject.put("msg","");

        System.out.println("get======="+body);
        JSONObject jsonObject1 = JSON.parseObject(body);
        String seqUuid = jsonObject1.getString("seqUuid");
        int opcode = jsonObject1.getInteger("opcode");
        statusMap.put(seqUuid,opcode);
        return jsonObject;
    }

    @DeleteMapping("/ipc/restful/mission")
    public Object delete(@RequestBody String body) {
        System.out.println("delete======="+body);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",true);
        jsonObject.put("msg","");
        return jsonObject;
    }



    @PostMapping("/ipc/restful/mission")
    public Object add(@RequestBody String body) {
        System.out.println("post======="+body);
         JSONObject jsonObject = new JSONObject();
         jsonObject.put("result",true);
         jsonObject.put("msg","");
        return jsonObject;
    }

    @GetMapping("/ipc/restful/mission")
    public Object get(@RequestBody String body) {
        System.out.println("get======="+body);
        JSONObject jsonObject1 = JSON.parseObject(body);
        String seqUuid = jsonObject1.getString("seqUuid");
        int opcode = jsonObject1.getInteger("opcode");
        if(opcode == 2){
            String result ="{\n" +
                    "    \"result\": true,\n" +
                    "    \"data\": {\n" +
                    "        \"hosts\": [\n" +
                    "            {\n" +
                    "                \"OS\": \"Linux\",\n" +
                    "                \"asset_type\": \"其它设备\",\n" +
                    "                \"device_model\": \"\",\n" +
                    "                \"firm\": \"factory\",\n" +
                    "                \"critical\": 0,\n" +
                    "                \"high\": 0,\n" +
                    "                \"ip\": \"10.121.201.24\",\n" +
                    "                \"level\": \"⽐较安全\",\n" +
                    "                \"info\": 0,\n" +
                    "                \"low\": 4,\n" +
                    "                \"mac\": \"00:0C:29:5B:07:93\",\n" +
                    "                \"middle\": 0,\n" +
                    "                \"port\": 0,\n" +
                    "                \"proto\": \"\",\n" +
                    "                \"score\": 90,\n" +
                    "                \"serial_num\": \"\",\n" +
                    "                \"service\": \"\",\n" +
                    "                \"serviceList\": [\n" +
                    "                    {\n" +
                    "                        \"port\": 22000,\n" +
                    "                        \"service\": \"snapenetio\",\n" +
                    "                        \"proto\": \"tcp\"\n" +
                    "                    }\n" +
                    "                ],\n" +
                    "                \"software\": \"\"\n" +
                    "            }\n" +
                    "        ],\n" +
                    "        \"vul\": [\n" +
                    "            {\n" +
                    "                \"cncve\": \"\",\n" +
                    "                \"cnnvd\": \"\",\n" +
                    "                \"cnvd\": \"\",\n" +
                    "                \"cve\": \"\",\n" +
                    "                \"cvss_score\": \"1\",\n" +
                    "                \"engine\": \"hse\",\n" +
                    "                \"first_type\": \"host\",\n" +
                    "                \"ip\": \"10.121.201.24\",\n" +
                    "                \"level\": \"L\",\n" +
                    "                \"link\": \"\",\n" +
                    "                \"port\": null,\n" +
                    "                \"second_type\": \"其他漏洞\",\n" +
                    "                \"service_category\": \"OpenSSL\",\n" +
                    "                \"vul_desc\": \"SSL是Netscape公司所提出的安全保密协议\",\n" +
                    "                \"vul_id\": \"69427\",\n" +
                    "                \"vul_name\": \"SSL证书信息泄漏\",\n" +
                    "                \"vul_result\": \"\",\n" +
                    "                \"vul_solution\": \"暂⽆数据。\",\n" +
                    "                \"request\": \"\",\n" +
                    "                \"response\": \"\"\n" +
                    "            }\n" +
                    "        ]\n" +
                    "    }\n" +
                    "}";
            JSONObject jsonObject = JSONObject.parseObject(result);
            return jsonObject;
        }else {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("result",true);
            Integer i = statusMap.get(seqUuid);
            if(i != null){
                jsonObject.put("progress","33");
                jsonObject.put("states",i);
                return jsonObject;
            }
            //1 获取任务状态
            Integer num = map.getOrDefault(seqUuid,1);
            //  1--等待中,2--执⾏中,3--⾃动结束,4-⼿动结束,6--⼿动暂停,7--系统暂停
            if(num == 1){
                jsonObject.put("progress","0");
                jsonObject.put("states",1);
            }
            if(num == 2){
                jsonObject.put("progress","20");
                jsonObject.put("states",2);
            }
            if(num >= 3){
                jsonObject.put("progress","100");
                jsonObject.put("states",3);
            }
            if(num >= 3){
                jsonObject.put("progress","100");
                jsonObject.put("states",3);
            }
            map.put(seqUuid,++num);

            return jsonObject;

        }


    }





}
